﻿
function StaffEditController($scope, service, $routeParams, $location, $http, $filter, $validator) {
    
    ///date picker
     $scope.openDayFrom = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.openedDayForm = true;
    };
    $scope.initDate = new Date('2016-15-20');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM', 'dd.MM.yyyy', 'shortDate', 'yyyy/MM/dd'];
    $scope.format = $scope.formats[4];
    //End
    // popup option
    var popupOptions = {
        appendTo: $("#popup-wrapper"),
        closeClass: 'b-close',
        speed: 300
    };
    $scope.path = "/StaffList";
    //end
    $scope.showTest = true;
    //========================================================================================================================================
    // Case Edit staff 
    if ($routeParams.type == "Edit") {
        //get store data of company
        $http.get(service.locationUrl + "/storelist/" + service.infoUser.Id, {})
            .success(function (data) {
                var allStores = data;

                $scope.allStores = allStores;

                //get store staff belong
                $http.get(service.locationUrl + "/staffstore/GetStoreOfStaff/" + $routeParams.Id, {})
                    .success(function (data) {
                        var storesOfStaff = data;

                        var isExist = function (store) {
                            if (storesOfStaff) {
                                for (var i = 0; i < storesOfStaff.length; i++) {
                                    if (storesOfStaff[i].CompanyStoreId == store.StoreId)
                                        return true;
                                }
                            }
                            return false;
                        }

                        //This is the time to convert form staffs' ids to object that renderable to to store checkbox list
                        _.each(allStores, function (store) {
                            //Find in the storesOfStaff, if it exist, just simple set the Checked property to true
                            store.Checked = isExist(store);
                        });

                        //Update the scope
                        $scope._update($scope);
                    })
                    .error(function (err) {

                        console.log("error", err);
                    });
                //End get

            })
            .error(function (err) {
                console.log("error", err);
            });
        //End get store
        //=================================================================================================================
        // get staff list by company id
        $http.get(service.locationUrl + "/companystaff/" + $routeParams.Id, {})
            .success(function (data) {
                //data.VisaExperationDate = $filter('date')(data.VisaExperationDate, 'yyyy-MM-dd');
                $scope.stafData = data;
            })
            .error(function (err) {

                console.log("error", err);
            });
        //End get staff
    }
    //End
    //=======================================================================================================================
    //Case New Staff
    if ($routeParams.type == "New")
    {
        $scope.showTest = false;
        $scope.stafData =
                          {
                              CompanyId: service.infoUser.Id,
                              Name: "",
                              NameKana: "",
                              FaceModel1: "",
                              FaceModel2: "",
                              WorkingStatus: 1,
                              ContractType: 1,
                              WorkVisa: 1,
                              VisaExperationDate: "",
                              RowStatus : 1
                          }
        //get store data of company
        $http.get(service.locationUrl + "/storelist/" + service.infoUser.Id, {})
            .success(function (data) {
                var allStores = data;

                $scope.allStores = allStores;

                var storesOfStaff = "";

                var isExist = function (store) {
                    if (storesOfStaff) {
                        for (var i = 0; i < storesOfStaff.length; i++) {
                            if (storesOfStaff[i].CompanyStoreId == store.StoreId)
                                return true;
                        }
                    }
                    return false;
                }

                //This is the time to convert form staffs' ids to object that renderable to to store checkbox list
                _.each(allStores, function (store) {
                    //Find in the storesOfStaff, if it exist, just simple set the Checked property to true
                    store.Checked = isExist(store);
                });

                //Update the scope
                $scope._update($scope);
            })
            .error(function (err) {
                console.log("error", err);
            });
        //End get store
    }
   //End New
    //======================================================================================================================
    // Update or Insert staff to datase dependent on case edit or new
    $scope.saveStaff = function () {
        $validator.validate($scope, "stafData")
            .success(function () {
                if ($routeParams.type == "Edit") {
                    $scope.title = "Do you want Save ?";
                }
                if ($routeParams.type == "New") {
                    $scope.title = "Do you want create new staff ?";
                }
                // popup detele
                $('.popup-delete-save').bPopup(popupOptions);
                // End

                $scope.action = function () {
                    //case edit staff update staff
                    if ($routeParams.type == "Edit") {
                        //obj staff
                        var objStaff =
                            {
                                CompanyId: $scope.stafData.CompanyId,
                                Name: $scope.stafData.Name,
                                NameKana: $scope.stafData.NameKana,
                                FaceModel1: $scope.stafData.FaceModel1,
                                FaceModel2: $scope.stafData.FaceModel2,
                                WorkingStatus: parseInt($scope.stafData.WorkingStatus),
                                ContractType: parseInt($scope.stafData.ContractType),
                                WorkVisa: parseInt($scope.stafData.WorkVisa),
                                VisaExperationDate: $scope.stafData.VisaExperationDate,
                                RowStatus: 1
                            }

                        var storesOfStaff = _.filter($scope.allStores, function (store) {
                            return store.Checked;
                        });

                        storesOfStaff = _.map(storesOfStaff, function (store) {
                            return store.StoreId;
                        });
                        objStaff.StoreIds = storesOfStaff;
                        //Put method update obj staff
                        $http.put(
                            service.locationUrl + "/companystaff/" + $routeParams.Id,
                            objStaff)
                        .success(function () {
                            $location.path($scope.path);
                        })
                        .error(function () { });
                        // end put
                    }
                    //end case
                    if ($routeParams.type == "New")
                    {
                        var objStaff = $scope.stafData;

                        var storesOfStaff = _.filter($scope.allStores, function (store) {
                            return store.Checked;
                        });

                        storesOfStaff = _.map(storesOfStaff, function (store) {
                            return store.StoreId;
                        });
                        objStaff.StoreIds = storesOfStaff;

                        console.log("aaa", objStaff);
                        $http.post(
                            service.locationUrl + "/companystaff",
                            objStaff)
                        .success(function () {
                            $location.path($scope.path);
                        })
                        .error(function () { });
                         //end post
                    }
                }
            }).error(function () {
                console.log("err");
            });
    }
    //=============================================================================================================================================
    // btn update row status = 0
    $scope.deleteStaff = function () {
     // popup detele
        $scope.title = "Do you want delete this staff ?";
        $('.popup-delete-save').bPopup(popupOptions);
    // End
        $scope.action = function () {
            //. get staff list by company id
            $http.get(service.locationUrl + "/companystaff/" + $routeParams.Id, {})
                .success(function (data) {
                    $scope.objDelete = data;
                    var objStaff = {
                      CompanyId: $scope.objDelete.CompanyId,
                      Name: $scope.objDelete.Name,
                      NameKana: $scope.objDelete.NameKana,
                      FaceModel1: $scope.objDelete.FaceModel1,
                      FaceModel2: $scope.objDelete.FaceModel2,
                      WorkingStatus: $scope.objDelete.WorkingStatus,
                      ContractType: $scope.objDelete.ContractType,
                      WorkVisa: $scope.objDelete.WorkVisa,
                      VisaExperationDate: $scope.objDelete.VisaExperationDate,
                      RowStatus: 0
                  }
                    //End get staff
                    console.log("awjkdakj", objStaff);
                    //Put method update obj staff
                    $http.put(
                        service.locationUrl + "/companystaff/" + $routeParams.Id,
                        objStaff)
                    .success(function () {
                        $location.path($scope.path);
                    })
                    .error(function () { });
                    // end put
                    console.log("delete");
                })
                .error(function (err) {

                    console.log("error", err);
                });
        }  
    }
}